package com.ruoyi.reservationserver.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.reservationserver.dao.AdminDao;
import com.ruoyi.reservationserver.entity.Admin;
import com.ruoyi.system.domain.StadiumStatistic;
import com.ruoyi.reservationserver.service.AdminService;
import com.ruoyi.system.service.CourtStatisticService;
import com.ruoyi.reservationserver.service.StadiumStatisticService;
import com.ruoyi.reservationserver.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理员表
(Admin)表控制层
 *
 * @author makejava
 * @since 2023-07-04 16:21:47
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Resource
    private AdminService adminService;

    @Resource
    private AdminDao adminDao;

    @Resource
    private CourtStatisticService courtStatisticService;

    @Resource
    private StadiumStatisticService stadiumStatisticService;

    /**
     * 分页获取用户数据
     *
     * @param page
     * @param count
     * @return
     */
    @GetMapping
    public Result index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count) {
        PageInfo<Admin> pageInfo = adminService.selectAdminWithPage(page, count);
        return Result.success(pageInfo);
    }

    /**
     * 通过管理员账号查询管理员
     *
     * @param account
     * @return
     */
    @GetMapping("selectAdminByAccount")
    public Result selectUserByAccount(@RequestParam String account) {
        List<Admin> adminList;
        if (account == null) {
            return Result.error("不能输入空信息");
        }
        adminList = adminDao.selectAdminByAccount(account);
        return Result.success(adminList);
    }

    /**
     * 新增数据
     *
     * @param admin 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public Result add(@RequestBody Admin admin) {

        if (admin == null) {
            return Result.error("输入不能为空");
        } else {
            this.adminService.insert(admin);

            QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
            wrapper.eq("stadium_statistic_id", 1);
            StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
            stadiumStatistic.setTotalAdminNum(adminService.count());
            stadiumStatisticService.update(stadiumStatistic);
            return Result.success();
        }
    }

    /**
     * 编辑数据
     *
     * @param admin 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Admin admin) {
        try {
            System.out.println(admin);
            if (admin == null) {
                return Result.error("数据不能为空");
            }
//            System.out.println(user.getUserId());
            if (admin.getAdminId() == null) {
                return Result.error("id不能为空");
            }
            this.adminService.update(admin);
            return Result.success();

        } catch (Exception e) {
            return Result.error("更新失败");
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/del")
    public Result deleteById(@RequestParam("adminId") Integer id) {

        try {
            if (this.adminService.deleteById(id)) {
                QueryWrapper<StadiumStatistic> wrapper = new QueryWrapper<>();
                wrapper.eq("stadium_statistic_id", 1);
                StadiumStatistic stadiumStatistic = stadiumStatisticService.getOne(wrapper);
                stadiumStatistic.setTotalAdminNum(adminService.count());
                stadiumStatisticService.update(stadiumStatistic);

            } else {
                return Result.error("500", "出错啦");
            }
            return Result.success();



        } catch (Exception e) {
            System.out.println(e);
            return Result.error("删除失败");
        }

    }

}

